package planosSaude;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import util.Data;
import util.PersistenceMechanismException;
import util.PersistenceMechanismRDBMS;
import util.RepositorioException;

public class RepositorioPlanosSaudeBDR implements RepositorioPlanosSaude {

	private static RepositorioPlanosSaudeBDR instance;
	private PersistenceMechanismRDBMS pm;
	
    private RepositorioPlanosSaudeBDR() {
        try {
            pm = PersistenceMechanismRDBMS.getInstance();
            pm.connect();
        }
        catch (Exception ex) {
            //System.out.println(ex.getMessage()); 
            ex.printStackTrace();
        }
    }

    public synchronized static RepositorioPlanosSaudeBDR getInstance() {
        if (instance == null) {
            instance = new RepositorioPlanosSaudeBDR();
        }
        return instance;
    }

    public void inserir(PlanoSaude planoSaude) throws RepositorioException {
    	 try {
             Statement statement = (Statement) pm.getCommunicationChannel();
             statement.executeUpdate("INSERT INTO planosSaude VALUES ('" +
                                     planoSaude.getCodigo() + "', '" +
                                     planoSaude.getNome()+ "', '" + 
                                     planoSaude.getTipo() +"', '" +
                                     planoSaude.getDataInscricao()+ "')");
         } catch(PersistenceMechanismException e){
             throw new RepositorioException(e);
         } catch (SQLException e) {
             throw new RepositorioException(e);
 	    } finally {
 		    try {
 				pm.releaseCommunicationChannel();
 			} catch (PersistenceMechanismException ex) {
 				throw new RepositorioException(ex);
 			}
         }
     }
    
    public void remover(long codigo) throws PlanoSaudeNaoEncontradoException, RepositorioException {
    	 try{
            Statement statement = (Statement) pm.getCommunicationChannel();
 		    int i = statement.executeUpdate("DELETE FROM planosSaude WHERE codigo='" + codigo + "'");
            if (i == 0) {
             	throw new PlanoSaudeNaoEncontradoException(codigo);
             }
 		} catch(PersistenceMechanismException e){
             throw new RepositorioException(e);
 		} catch(SQLException e){
             throw new RepositorioException(e);            
 		} finally {
 			try {
 				pm.releaseCommunicationChannel();
 			} catch (PersistenceMechanismException ex) {
 				throw new RepositorioException(ex);
 			}
 		}
 	}
		
			
	public void atualizar(PlanoSaude planoSaude) throws PlanoSaudeNaoEncontradoException, RepositorioException {
		 try {
	    	    Statement statement = (Statement) pm.getCommunicationChannel();
	            int i = statement.executeUpdate("UPDATE planosSaude SET nome = '" + planoSaude.getNome() + 
	           	                                "', tipo = '" + planoSaude.getTipo()+ 
	           	                                "', dataInscricao = '" + planoSaude.getDataInscricao().toString() + 
	           	                                "' WHERE codigo = '" +planoSaude.getCodigo()+ "'");
	            if (i == 0) {
	            	throw new PlanoSaudeNaoEncontradoException(planoSaude.getCodigo());
	            }
			} catch(PersistenceMechanismException e){
	            throw new RepositorioException(e);
			} catch (SQLException e) {
			    throw new RepositorioException(e);
		    } finally {
		    	try {
					pm.releaseCommunicationChannel();
				} catch (PersistenceMechanismException ex) {
					throw new RepositorioException(ex);
				}
			}
		
	}

	public boolean existe(long codigo) throws RepositorioException {
		 boolean resposta = false;
	        try {
	            Statement statement = (Statement) pm.getCommunicationChannel();
	            ResultSet resultset = statement.executeQuery("SELECT * FROM planosSaude WHERE codigo = '" + codigo + "'");
	            resposta = resultset.next();
				resultset.close();
			} catch(PersistenceMechanismException e){
	            throw new RepositorioException(e);
	        } catch (SQLException e) {
				throw new RepositorioException(e);
			} finally {
				try {
					pm.releaseCommunicationChannel();
				} catch (PersistenceMechanismException ex) {
					throw new RepositorioException(ex);
				}
			}
	        return resposta;
	}

	

	public PlanoSaude procurar(long codigo) throws PlanoSaudeNaoEncontradoException, RepositorioException {
		PlanoSaude planosaude = null;
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM planosSaude WHERE codigo = '" + codigo + "'");
            if (resultset.next()) {
                planosaude = new PlanoSaude(resultset.getLong("codigo"), 
                		                    resultset.getString("nome"),
                		                    resultset.getString("tipo"), 
                		                    new Data(resultset.getString("dataInscricao")));
            } else {
            	throw new PlanoSaudeNaoEncontradoException(codigo);
            }
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		return planosaude;
	}

	public RepositorioPlanosSaude procurar(PlanoSaude planoSaude) throws RepositorioException, PlanoSaudeEncontradoException, PlanoSaudeNaoEncontradoException {
RepositorioPlanosSaude resposta = new RepositorioPlanosSaudeLista();
		
		try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM planosSaude");
            while (resultset.next()) {
            	planoSaude = new PlanoSaude(resultset.getLong("codigo"), 
            								resultset.getString("nome"),
            								resultset.getString("tipo"),
            								new Data(resultset.getString("dataInscricao")));
                resposta.inserir(planoSaude);
            }
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
        return resposta;
	}
	public IteratorPlanosSaude getIterator() throws RepositorioException {
		return this.getPlanosSaude().getIterator();
	}

	public RepositorioPlanosSaude getPlanosSaude() throws RepositorioException {
		RepositorioPlanosSaude resposta = new RepositorioPlanosSaudeLista();
	    try {
	        Statement statement = (Statement) pm.getCommunicationChannel();
	        ResultSet resultset = statement.executeQuery("SELECT * FROM planosSaude");
	        while (resultset.next()) {	        	
				PlanoSaude consulta = new PlanoSaude(
						resultset.getLong("codigo"),
						resultset.getString("nome"),
						resultset.getString("tipo"),
						new Data(resultset.getString("dataInscricao"))
	        	);            	
	            resposta.inserir(consulta);
	        }
			resultset.close();
		} catch(PersistenceMechanismException e){
	        throw new RepositorioException(e);
	    } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
	    return resposta;
	}
}
